/* global document */
import React from 'react'
import PropTypes from 'prop-types'
import {AutoComplete, Button, Col, DatePicker, Divider, Form, Icon, Input, Row, Select} from 'antd'
import classnames from 'classnames'
import styles from './Filter.less'
import {message} from "antd/lib/index"

const FormItem = Form.Item
const {Option} = Select

const ColProps = {
  xs: 6,
  sm: 6,
  style: {
    marginBottom: 6,
  },
}

const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18},
  style: {
    marginBottom: 10,
  },
}

const status = [
  {
    id: 1,
    name: '群聊总数',
    color: '#E03636',
    icon: 'team',
    value: 10010,
    count: 0,
  },
  {
    id: 2,
    name: '群成员总数',
    color: 'rgb(47, 194, 91)',
    icon: 'team',
    value: 10000,
    count: 0,
  },
  {
    id: 3,
    name: '男',
    color: '#4cb4e7',
    icon: 'man',
    value: 1,
    count: 0,
  },
  {
    id: 4,
    name: '女',
    color: '#FF6E97',
    icon: 'woman',
    value: 2,
    count: 0,
  },
  {
    id: 5,
    name: '未知',
    color: 'rgba(0, 0, 0, 0.45)',
    icon: 'question',
    value: 0,
    count: 0,
  },
]

const getStat = (stat, item, total) => {
  if (stat && stat.length > 0) {
    if (item.value === 10010) {
      item.count = total
    } else if (item.value === 10000) {
      let total = 0
      stat.map(item => {
        total += item.y ? parseInt(item.y) : 0
      })
      item.count = total
    } else {
      let tmp = stat.filter(item => parseInt(item.x) === item.value)
      if (tmp.length > 0) {
        item.count = tmp.y
      }
    }
    return item.count
  }
  return 0
}

const Filter = ({
                  dataSource,
                  handleSearch,
                  expandForm,
                  onFilterChange,
                  toggleForm,
                  stat,
                  total = 0,
                  form: {
                    getFieldDecorator,
                    getFieldsValue,
                    setFieldsValue,
                  },
                }) => {

  const handleFields = (fields) => {
    const {minCtime, maxCtime} = fields
    if (minCtime) {
      fields.minCtime = minCtime.format('YYYY-MM-DD HH:mm:ss')
    }
    if (maxCtime) {
      fields.maxCtime = maxCtime.format('YYYY-MM-DD HH:mm:ss')
    }
    return fields
  }

  const handleFormReset = () => {
    const fields = getFieldsValue()
    for (let item in fields) {
      if ({}.hasOwnProperty.call(fields, item)) {
        if (fields[item] instanceof Array) {
          fields[item] = []
        } else {
          fields[item] = undefined
        }
      }
    }
    setFieldsValue(fields)
    handleSubmit()
  }

  const valid = fields => {
    const {type, keyword} = fields

    if (!type && keyword) {
      message.warn("请选择查询类型")
      return false
    }

    return true
  }

  const handleSubmit = () => {
    let fields = getFieldsValue()
    if (!valid(fields)) {
      return
    }
    fields = handleFields(fields)
    onFilterChange(fields)
  }

  const simpleFields = [
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="群聊名称">
          {getFieldDecorator('chatroomName')(<Input placeholder="请输入群聊名称"/>)}
        </FormItem>
      </Col>
    ),
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="归属微信">
          {getFieldDecorator('belongWxId')(<Input placeholder="请输入归属微信号"/>)}
        </FormItem>
      </Col>
    ),
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="归属员工">
          {getFieldDecorator('belongEmpId')(
            <AutoComplete
              dataSource={dataSource.map(item => <Option value={item.id}>{item.name}</Option>)}
              style={{width: '100%'}}
              onSearch={handleSearch}
              placeholder="员工编号或名称"
            />,
          )}
        </FormItem>
      </Col>
    ),
  ]

  const renderSimpleForm = () => (
    <Form layout="horizontal">
      <Row gutter={24}>
        {simpleFields}
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem label={''}>
            <Button onClick={handleSubmit} className="margin-right" type="primary" htmlType="submit">查询</Button>
            <Button onClick={handleFormReset}>重置</Button>
            <a style={{marginLeft: 8}} onClick={toggleForm}>展开 <Icon type="down"/></a>
          </FormItem>
        </Col>
      </Row>
    </Form>
  )

  const renderAdvancedForm = () => (
    <Form layout="horizontal">
      <Row gutter={24}>
        {simpleFields}
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem label="inline" {...formItemLayout} label="群人数">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('minChatroomPeopleNum')(<Input placeholder="最小数"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('maxChatroomPeopleNum')(<Input placeholder="最大数"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="是否在本群">
            {getFieldDecorator('isInChatroom')(
              <Select allowClear placeholder="请选择">
                <Option value={1}>{'在本群'}</Option>
                <Option value={0}>{'不在本群'}</Option>

              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="群主微信号">
            {getFieldDecorator('chatroomOwnerWxId')(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 8}} md={4} sm={24}>
          <FormItem labelCol={{span: 5}} wrapperCol={{span: 18}} label="录入系统时间">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('minCtime')(<DatePicker style={{width: '100%'}} placeholder="开始时间"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('maxCtime')(<DatePicker style={{width: '100%'}} placeholder="结束时间"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
        <Col span={4}>
          <Button className="margin-right" style={{marginLeft: '15%'}} type="primary"
                  onClick={handleSubmit}>查询</Button>
          <Button onClick={handleFormReset}>重置</Button>
          <a style={{marginLeft: 8}} onClick={toggleForm}>收起 <Icon type="up"/></a>
        </Col>
      </Row>
    </Form>
  )

  const renderForm = () => {
    return expandForm ? renderAdvancedForm() : renderSimpleForm()
  }

  return (
    <div>
      <Row gutter={24}>
        {status.map(item => (
          <Col {...ColProps} xl={{span: 4}} md={{span: 4}} sm={{span: 12}}>
            <div className={classnames(styles['flex-status-bar'])} style={{color: `${item.color}`}}>
              <Icon type={item.icon} style={{fontSize: 20}} theme="outlined"/>&nbsp;
              <span>{item.name}：<span style={{fontWeight: 'bold', fontSize: 18}}>{getStat(stat, item, total)}</span></span>
            </div>
          </Col>
        ))}
        <Divider/>
      </Row>
      {renderForm()}
    </div>
  )
}

Filter.propTypes = {
  onAdd: PropTypes.func,
  form: PropTypes.object,
  filter: PropTypes.object,
  onFilterChange: PropTypes.func,
}

export default Form.create()(Filter)
